package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ConstanceVariable;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfigDomain;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author: liyang
 * @date: 2021/11/11 20:33
 */
@Component
public abstract class SaveNodeSinglePropertiesProcessor implements CustomPropertiesProcessor {

    public abstract String getPropertiesName();

    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            JsonNode singleProperties = properties.get(getPropertiesName());
            return nonNull(singleProperties);
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        JsonNode jsonNode = context.getElementNode();
        JsonNode properties = jsonNode.get("properties");
        if (Objects.nonNull(properties)){
            JsonNode singleProperties = properties.get(getPropertiesName());
            if (Objects.nonNull(singleProperties)){
                String value = singleProperties.textValue();
                ModelCustomConfigDomain modelCustomConfigDomain = new ModelCustomConfigDomain();
                modelCustomConfigDomain.setDeploymentId(context.getDeployId());
                modelCustomConfigDomain.setType(getPropertiesName());
                modelCustomConfigDomain.setNodeCode(context.getNodeCode());
                modelCustomConfigDomain.setConfig(value);
                modelCustomConfigDomain.save();
            }
        }
    }
}
